#!groovy
/*
 * Copyright 2020 SpotBugs plugin contributors
 *
 * This file is part of IntelliJ SpotBugs plugin.
 *
 * IntelliJ SpotBugs plugin is free software: you can redistribute it 
 * and/or modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation, either version 3 of 
 * the License, or (at your option) any later version.
 *
 * IntelliJ SpotBugs plugin is distributed in the hope that it will
 * be useful, but WITHOUT ANY WARRANTY; without even the implied 
 * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 * See the GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with IntelliJ SpotBugs plugin.
 * If not, see <http://www.gnu.org/licenses/>.
 */

import org.jetbrains.intellij.platform.gradle.TestFrameworkType

buildscript {
    ext.kotlin_version = '2.1.10'
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
    }
}

plugins {
    id "org.jetbrains.intellij.platform" version "2.2.1"
    id 'jacoco'
}

apply plugin: 'maven-publish'
apply plugin: 'kotlin'
apply plugin: 'java'

java {
}

repositories {
    mavenLocal()
    mavenCentral()
    intellijPlatform {
        defaultRepositories()
    }
}

intellijPlatform {
    pluginConfiguration {
        name = 'spotbugs-idea'
    }
}

jacoco {
    toolVersion = "0.8.12"
}

runIde {
    systemProperties(["jdk.util.zip.ensureTrailingSlash":false])
}

configurations {
    thirdPartyPlugins
    fbContribJava6
}

dependencies {
    intellijPlatform {
        def type = providers.gradleProperty('platformType')
        def version = providers.gradleProperty('platformVersion')

        create(type, version)

        bundledPlugin 'com.intellij.java'

        testFramework TestFrameworkType.Platform.INSTANCE
        testFramework TestFrameworkType.Plugin.Java.INSTANCE
    }
    implementation ('com.github.spotbugs:spotbugs:4.9.0') {
        exclude group: 'xml-apis', module: 'xml-apis'
        exclude group: 'org.apache.logging.log4j', module: 'log4j-api'
        exclude group: 'org.apache.logging.log4j', module: 'log4j-core'
        exclude group: 'org.slf4j', module: 'slf4j-api'
    }
    implementation 'net.sf.saxon:Saxon-HE:12.5'
    implementation 'org.jsoup:jsoup:1.18.3'
    implementation 'info.clearthought:table-layout:4.3.0'
    testImplementation 'junit:junit:4.13.2'
    testImplementation 'org.assertj:assertj-core:3.27.3'
    testImplementation 'org.mockito:mockito-core:5.15.2'
    thirdPartyPlugins 'com.mebigfatguy.fb-contrib:fb-contrib:7.6.9'
    thirdPartyPlugins 'com.h3xstream.findsecbugs:findsecbugs-plugin:1.13.0'
    fbContribJava6 'com.mebigfatguy.fb-contrib:fb-contrib:6.2.1'
    implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8:$kotlin_version"
}

tasks.register('copyGradleProperties', Copy) {
    description = 'Copy gradle.properties to project resources.'
    from 'gradle.properties'
    into 'build/resources/main/org/jetbrains/plugins/spotbugs/common'
    rename { _ -> 'version.properties' }
}

tasks.register('downloadThirdPartyPlugins', Copy) {
    description = 'Downloads third-party plugins Find Security Bugs and FB-Contrib.'
    from configurations.thirdPartyPlugins
    from configurations.fbContribJava6
    into 'build/resources/main/org/jetbrains/plugins/spotbugs/plugins'
}

tasks.register('copyThirdPartyPlugins', Copy) {
    dependsOn(downloadThirdPartyPlugins, prepareSandbox)
    description = 'Copy plugins into sandbox.'
    from 'build/resources/main/org/jetbrains/plugins/spotbugs/plugins'
    into 'build/idea-sandbox/plugins/spotbugs-idea/customPlugins'
}

tasks.register('deleteThirdPartyPlugins', Delete) {
    delete 'build/resources/main/org/jetbrains/plugins/spotbugs/plugins'
}

tasks.compileJava.dependsOn(downloadThirdPartyPlugins, copyGradleProperties)
tasks.buildPlugin.dependsOn(copyThirdPartyPlugins)
tasks.jacocoTestReport.dependsOn(deleteThirdPartyPlugins)
tasks.jacocoTestCoverageVerification.dependsOn(deleteThirdPartyPlugins)

test {
    def ideaHomePath = System.getenv().get('IDEA_HOME_PATH') ?: project.findProperty('idea.home.path')
    if (ideaHomePath == null) {
        throw new RuntimeException("Please set the 'idea.home.path' property at gradle.properties")
    }
    systemProperty "idea.home.path", ideaHomePath
    finalizedBy jacocoTestReport
}

jacocoTestReport {
    dependsOn test
    reports {
        xml.required = true
        html.required = false
        csv.required = false
    }
}

wrapper {
    gradleVersion = '8.12'
}

kotlin {
    jvmToolchain(17)
}
